import auth from './auth'
import store from '@/store'
export default{
    validateLogin:async (to,from,next)=>{
        //1.判断是否登陆过 =>如果登陆过...
        //                =>如果没有登陆过 判断用户是否需要登陆
        // console.log(to.name)
      
        try{
          //为什么this.$store不行？
         await store.dispatch('validateUser')
          if(to.name=='/login'){
            next('/')
          }else{
            next()
          }
        }catch(e){
          //判断是否需要登陆 只在course里面设置了meta标识
          let needLogin = to.matched.some(item=>item.meta.needLogin)
          if(needLogin){
            next('/login')
          }else{
            next()
          }
        }
      
      },
      profileAuth:function(to,from,next){
          if(store.state.hasPermission&&store.state.user.menuList){
              if(!store.state.menuPermission){
                  let list = store.state.user.menuList.map(item=>item.auth);//从后端找出有权限的路由
                  let newRoutes = auth.filter(item=>list.includes(item.name))//根据后端返回的数据进行筛选，需要显示在页面上的动态路由
                store.commit('setMenuList')
                this.addRoutes(newRoutes);
                //新增的路由下一轮才会生效
                next({...to,replace:true}) //导航不会留下history记录
                }else{
                    next()
                }
          }else{
              next()
          }
      }
}